﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessLogic.Helper;
using BusinessLogic.Controller;
using DataEntity;
using JScripter;

public partial class AjaxPages_Property_AddPropertyStep1 : AjaxPage
{
    private int PropertyTypeID
    {
        get
        {
            if (Request.Params["ptp"] != null || AjaxState.ContainsKey("ptp"))
            {
                if (!AjaxState.ContainsKey("ptp"))
                {
                    AjaxState["ptp"] = Request.Params["ptp"];
                }
                return Convert.ToInt32(AjaxState["ptp"]);
            }
            else
            {
                return 0;
            }
        }
    }

    private string PropertyTypeName
    {
        get
        {
            if (PropertyTypeID > 0)
            {
                var data = new PropertyTypeController().GetbyPropertyTypeID(PropertyTypeID);
                if (data.Count > 0)
                {
                    return data[0].Name;
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
        BindAjaxEvents();
    }

    private void BindAjaxEvents()
    {
        ddCity.Attributes["onchange"] = objLoader.DropDownChangeEventScript(ddCity, "#localitydd", ResolveUrl("~/AjaxPages/ControlLoader/DControl.aspx" + "?cid="));
    }

    private void BindData()
    {
        BindBedrooms();
        BindMeasurement();
        BindPrice();
        BindPropertyFeature();
        BindTotalRooms();
        BindWashrooms();
        BindLocality();
        BindCity();
        BindQualityRatting();
        BindTransactionType();
        BindPossession();
        BindPropertyAge();
        BindPropertyOwnership();
        PropertyHelper.BindMeasurment(ddMinMaxArea);
    }
    private void BindPropertyFeature()
    {
        divBathrooms.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddBathroom.ClientID);
        PropertyHelper.BindInterger(ddBathroom, 20);
    }
    private void BindBedrooms()
    {
        divBedrooms.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddBedroom.ClientID);
        PropertyHelper.BindInterger(ddBedroom, 20);
    }
    private void BindWashrooms()
    {
        divWashrooms.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddWashroom.ClientID);
        PropertyHelper.BindInterger(ddWashroom, 20);
    }

    private void BindTotalRooms()
    {
        divtotalroom.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddtotalroom.ClientID);
        PropertyHelper.BindInterger(ddtotalroom, 30);
    }

    private void BindQualityRatting()
    {
        divqualityratting.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, ddqualityratting.ClientID);
        PropertyHelper.BindInterger(ddqualityratting, 5);
    }

    private void BindMeasurement()
    {
        PropertyHelper.BindMeasurment(ddbuiltupareaMeasure);
        PropertyHelper.BindMeasurment(ddCarpetAreaMeasure);
        PropertyHelper.BindMeasurment(ddPlotSize);

        divbuilduparea.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, txtbuiltuparea.ClientID);
        divplotsize.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, txtPlotSize.ClientID);
        divcarpetarea.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, txtCarpetArea.ClientID);
        divminmax.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, txtMinimumArea.ClientID);
        divminmax.Visible = new PropertyTypeControlSettingController().IsVisible(PropertyTypeID, txtMaximumArea.ClientID);
    }

    private void BindPrice()
    {
        PropertyHelper.BindInterger(ddexpLacs, 100);
        PropertyHelper.BindInterger(ddexpThousands, 100);
        PropertyHelper.BindInterger(ddexpCrores, 100);
    }

    private void BindCity()
    {
        PropertyHelper.BindCity(ddCity);
    }

    private void BindLocality()
    {
        ddLocality.Enabled = false;
        PropertyHelper.BindLocality(ddLocality);
    }

    private void BindTransactionType()
    {

        new TransactionTypeController().BindTransactionType(ddTransactionType);
    }

    private void BindPropertyOwnership()
    {
        new PropertyOwnershipController().BindPropertyOwnership(ddPropertyOwnership);
    }

    private void BindPropertyAge()
    {
        new PropertyAgeController().BindPropertyAge(ddPropertyAge);
    }

    private void BindPossession()
    {
        new PropertyProsessionController().BindPropertyProsession(ddPossession);
    }
    protected void Add_Click(object sender, AjaxControl.AjaxEventArg e)
    {
        Address address = new Address();
        string address1 = HtmlHelper.ControlValue(txtAddress.ClientID);
        if (address1 != null)
            address.Address1 = HtmlHelper.ControlValue(txtAddress.ClientID);
        address.AddressID = Guid.NewGuid();
        string cityID = HtmlHelper.ControlValue(ddCity.ClientID);
        if (cityID != "0" && cityID != "")
            address.CityID = new Guid(cityID);
        string localityID = HtmlHelper.ControlValue(ddLocality.ClientID);
        if (localityID != "0" && localityID != "")
            address.LocalityID = new Guid(localityID);
        address.ModifiedDate = DateTime.Now;
        address.PinCode = HtmlHelper.ControlValue(txtPincode.ClientID);

        AreaDetail areaDetail = new AreaDetail();
        areaDetail.AreaDetailID = Guid.NewGuid();
        areaDetail.BuildUpArea = HtmlHelper.ControlValue(txtbuiltuparea.ClientID);
        areaDetail.BuildUpAreaIn = HtmlHelper.ControlValue(ddbuiltupareaMeasure.ClientID);
        areaDetail.CarpetArea = HtmlHelper.ControlValue(txtCarpetArea.ClientID);
        areaDetail.CarpetAreaIn = HtmlHelper.ControlValue(ddCarpetAreaMeasure.ClientID);
        string maximumArea = HtmlHelper.ControlValue(txtMaximumArea.ClientID);
        if (maximumArea != "")
            areaDetail.MaximumArea = Convert.ToInt32(maximumArea);


        string minimumArea = HtmlHelper.ControlValue(txtMinimumArea.ClientID);
        if (minimumArea != "")
            areaDetail.MinimumArea = Convert.ToInt32(minimumArea);


        areaDetail.MinMaxIn = HtmlHelper.ControlValue(ddMinMaxArea.ClientID);
        areaDetail.ModifiedDate = DateTime.Now;
        areaDetail.NumberOfProperty = 1;
        areaDetail.PlotArea = HtmlHelper.ControlValue(txtPlotSize.ClientID);
        areaDetail.PlotAreaIn = HtmlHelper.ControlValue(ddPlotSize.ClientID);

        TransactionDetail transactionDetail = new TransactionDetail();
        transactionDetail.ModifiedDate = DateTime.Now;


        string propertyAgeID = HtmlHelper.ControlValue(ddPropertyAge.ClientID);
        if (propertyAgeID != "0" && propertyAgeID != "")
            transactionDetail.PropertyAgeID = Convert.ToInt32(propertyAgeID);

        string propertyOwnershipID = HtmlHelper.ControlValue(ddPropertyOwnership.ClientID);
        if (propertyOwnershipID != "0" || propertyOwnershipID != "")
            transactionDetail.PropertyOwnershipID = Convert.ToInt32(propertyOwnershipID);


        string propertyProsessionID = HtmlHelper.ControlValue(ddPossession.ClientID);
        if (propertyProsessionID != "0" && propertyProsessionID != "")
            transactionDetail.PropertyProsessionID = Convert.ToInt32(propertyProsessionID);

        transactionDetail.TransactionDetailID = Guid.NewGuid();

        string transactionTypeID = HtmlHelper.ControlValue(ddTransactionType.ClientID);
        if (transactionTypeID != "0" && transactionTypeID != "")
            transactionDetail.TransactionTypeID = Convert.ToInt32(transactionTypeID);


        PropertyDescription propertyDescription = new PropertyDescription();
        propertyDescription.Description = HtmlHelper.ControlValue(txtPropertyDescription.ClientID);
        propertyDescription.ModifiedDate = DateTime.Now;
        propertyDescription.PropertyDescriptionID = Guid.NewGuid();
        propertyDescription.SocityName = HtmlHelper.ControlValue(txtSocietyName.ClientID);

        Property property = new Property();
        property.AddressID = address.AddressID;
        property.AreaDetailID = areaDetail.AreaDetailID;


        string bathroom = HtmlHelper.ControlValue(ddBathroom.ClientID);
        if (bathroom != "")
            property.Bathroom = Convert.ToInt32(bathroom);

        string Bedroom = HtmlHelper.ControlValue(ddBedroom.ClientID);
        if (Bedroom != "")
            property.Bedroom = Convert.ToInt32(Bedroom);

        property.IsDeleted = false;
        property.ModifiedDate = DateTime.Now;

        string Crores = HtmlHelper.ControlValue(ddexpCrores.ClientID);
        if (Crores != "")
            property.Price = Convert.ToInt32(Crores);


        string lacs = HtmlHelper.ControlValue(ddexpLacs.ClientID);
        if (lacs != "")
            property.Price += Convert.ToInt32(lacs);


        string thousand = HtmlHelper.ControlValue(ddexpThousands.ClientID);
        if (thousand != "")
            property.Price += Convert.ToInt32(thousand);

        property.PropertyDescriptionID = propertyDescription.PropertyDescriptionID;
        property.PropertyID = Guid.NewGuid();
        property.QualityRatting = HtmlHelper.ControlValue(ddqualityratting.ClientID);
        
        string totalRoom = HtmlHelper.ControlValue(ddtotalroom.ClientID);
        if (totalRoom != "0" && totalRoom != "")
            property.TotalRoom = Convert.ToInt32(totalRoom);
        
        property.TransactionDetailID = transactionDetail.TransactionDetailID;


        string washRoom = HtmlHelper.ControlValue(ddWashroom.ClientID);
        if (washRoom != "0" && washRoom != "")
            property.WashRoom = Convert.ToInt32(washRoom);

        property.LoginID = new LoginHelper().LoginID;
        property.PropertyTypeID = PropertyTypeID;
        property.PostDate = DateTime.Now;
        property.ExpireDate = DateTime.Now.AddMonths(3);
        property.IsApproved = true;
        PropertyHelper.PropertyAdd(property, address, areaDetail, transactionDetail, propertyDescription);

        Response.Redirect("~/AjaxPages/Property/AddPropertyStep2.aspx" + "?pid=" + property.PropertyID.ToString());

    }
}